#!/bin/bash
# source ./brances_sort_function.sh ${bulidScriptCommon_dir_Absolute}
:<<!
一些公共方法
!

#echo "===========进入脚本$0==========="
#echo "===========引入脚本 source ./brances_sort_function.sh ==========="

JQ_EXEC=`which jq`

bulidScriptCommon_dir_Absolute=$1


function addBranchArray() {
    while [ -n "$1" ]
    do
            case "$1" in
                    -addedBranchMaps1|--added-branchMap-array1) addBranchMapArray1=$2; shift 2;;
                    -addedBranchMaps2|--added-branchMap-array2) addBranchMapArray2=$2; shift 2;;
                    --) break ;;
                    *) echo $1,$2; break ;;
            esac
    done
    
    branchMapArrayAddedResult=()
    
    addBranchMapArray1=(${addBranchMapArray1})
    #echo "addBranchMapArray1=${addBranchMapArray1}"
    addBranchMapArray2=(${addBranchMapArray2})
    #echo "addBranchMapArray2=${addBranchMapArray2[*]}"

    branchCount1=${#addBranchMapArray1[@]}
    #echo "branchCount1=${branchCount1}"
    for ((i=0;i<branchCount1;i++))
    do
        iBranchMap=${addBranchMapArray1[i]}
        branchMapArrayAddedResult[${#branchMapArrayAddedResult[@]}]=${iBranchMap}
    done
    
    
    branchCount2=${#addBranchMapArray2[@]}
    #echo "branchCount2=${branchCount2}"
    for ((i=0;i<branchCount2;i++))
    do
        iBranchMap=${addBranchMapArray2[i]}
        branchMapArrayAddedResult[${#branchMapArrayAddedResult[@]}]=${iBranchMap}
    done
    
    #echo "branchMapArrayAddedResult=${branchMapArrayAddedResult[*]}"
    #echo "branchMapArrayAddedResult_count=${#branchMapArrayAddedResult[@]}"
}


# 排序branch数组(按类型)
function sortBranchArrayByType() {
    while [ -n "$1" ]
    do
            case "$1" in
                    -branchMaps|--branchMap-array) branchMapArray=$2; shift 2;;
                    --) break ;;
                    *) echo $1,$2; break ;;
            esac
    done
    
    branchMapArray_all_new_withjq_result=()
    #echo "branchMapArray=${branchMapArray}"
            
    branchNamesString=$(echo ${branchMapArray} | ${JQ_EXEC} -r '.[].name') # -r 去除字符串引号
    branchNameArray=(`echo ${branchNamesString}`)
    branchCount=${#branchNameArray[@]}
    for ((i=0;i<branchCount;i++))
    do
        iBranchMap=$(echo ${branchMapArray} | ${JQ_EXEC} -r ".[$((i))]") # -r 去除字符串引号
        #echo "iBranchMap=${iBranchMap}"
        # [shell替换和去掉换行符](http://www.noobyard.com/article/p-ahlemikj-nz.html)
        FileContent=$(echo ${iBranchMap} |sed 's/ /\n/g'|awk '{{printf"%s",$0}}')
        
        # [Shell 命令变量去除空格方法](https://blog.csdn.net/jjc120074203/article/details/126663391)
        FileContent=${FileContent// /}
            
        branchType=$(echo ${iBranchMap} | ${JQ_EXEC} -r ".type") # -r 去除字符串引号
        if [ "${branchType}" == "hotfix" ]; then
            branchMapArray_hotfix[${#branchMapArray_hotfix[@]}]="${FileContent}"
        elif [ "${branchType}" == "feature" ]; then
            branchMapArray_feature[${#branchMapArray_feature[@]}]="${FileContent}"
        elif [ "${branchType}" == "optimize" ]; then
            branchMapArray_optimize[${#branchMapArray_optimize[@]}]="${FileContent}"
        else
            branchMapArray_other[${#branchMapArray_other[@]}]="${FileContent}"
        fi
    done
    
#    echo "branchMapArray_hotfix=${branchMapArray_hotfix[*]}"
#    echo "branchMapArray_feature=${branchMapArray_feature[*]}"
#    echo "branchMapArray_optimize=${branchMapArray_optimize[*]}"
#    echo "branchMapArray_other=${branchMapArray_other[*]}"
#    echo "branchMapArray_hotfix_count=${#branchMapArray_hotfix[@]}"
#    echo "branchMapArray_feature_count=${#branchMapArray_feature[@]}"
#    echo "branchMapArray_optimize_count=${#branchMapArray_optimize[@]}"
#    echo "branchMapArray_other_count=${#branchMapArray_other[@]}"
    
    #echo "=============== 添加 hotfix 分支 ==============="
    addBranchArray -addedBranchMaps1 "${branchMapArray_all_new[*]}" -addedBranchMaps2 "${branchMapArray_hotfix[*]}"
    branchMapArray_all_new="${branchMapArrayAddedResult[*]}"
    
    #echo "=============== 添加 feature 分支 ==============="
    addBranchArray -addedBranchMaps1 "${branchMapArray_all_new[*]}" -addedBranchMaps2 "${branchMapArray_feature[*]}"
    branchMapArray_all_new="${branchMapArrayAddedResult[*]}"

    #echo "=============== 添加 optimize 分支 ==============="
    addBranchArray -addedBranchMaps1 "${branchMapArray_all_new[*]}" -addedBranchMaps2 "${branchMapArray_optimize[*]}"
    branchMapArray_all_new="${branchMapArrayAddedResult[*]}"
    
    #echo "=============== 添加 other 分支 ==============="
    addBranchArray -addedBranchMaps1 "${branchMapArray_all_new[*]}" -addedBranchMaps2 "${branchMapArray_other[*]}"
    branchMapArray_all_new="${branchMapArrayAddedResult[*]}"
    
    
    #branchMapArray_all_new=$(echo ${branchMapArray_all_new[*]} | ${JQ_EXEC} -r) # -r 去除字符串引号
    branchMapArray_all_new=($branchMapArray_all_new)
    #echo "branchMapArray_all_new_nojq============${branchMapArray_all_new[*]}"
    #echo "branchMapArray_all_new_nojq_count=${#branchMapArray_all_new[@]}"
    
    source ${bulidScriptCommon_dir_Absolute}/a_function.sh ${bulidScriptCommon_dir_Absolute}
    getJsonStringFromArray ${branchMapArray_all_new[*]}
    dirFileContentJsonStrings=${arrayJsonResultString}
    #echo "dirFileContentJsonStrings=${dirFileContentJsonStrings}"

    branchMapArray_all_new_withjq_result=$(echo "${dirFileContentJsonStrings}" | ${JQ_EXEC} -r '.')
    #echo "branchMapArray_all_new_withjq_result============${branchMapArray_all_new_withjq_result}"
    branchNamesString=$(echo ${branchMapArray_all_new_withjq_result} | ${JQ_EXEC} -r '.[].name') # -r 去除字符串引号
    branchNameArray=(`echo ${branchNamesString}`)
    branchCount=${#branchNameArray[@]}
    #echo "branchMapArray_all_new_withjq_result_count=${branchCount}"
    
    #i=2
    #iBranchMap=$(echo "${branchMapArray_all_new_withjq_result}" | ${JQ_EXEC} -r ".[$((i))]") # -r 去除字符串引号
    #echo "$i iBranchMap=${iBranchMap[*]}"
}


